package cn.liziguo.tank.script;

import cn.liziguo.tank.TankGameApplication;
import cn.liziguo.tank.constants.GameEnum;
import cn.liziguo.tank.factory.MapFactory;
import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.entity.component.Component;
import javafx.util.Duration;

/**
 * @author Liziguo
 * @date 2022/7/19 15:08
 */
public class MonitorWinComponent extends Component {

    public static final int NEXT_LEVEL_DELAY = 3000;

    @Override
    public void onUpdate(double tpf) {
        if (FXGL.getGameWorld().getGroup(GameEnum.ENEMY).getSize() == 0) {
            // 把家设置为不可碰撞的状态 以免上一秒赢了下一秒就输了
            FXGL.getGameWorld().getGroup(GameEnum.HOME).getEntitiesCopy().forEach(e -> e.setType(null));
            entity.removeFromWorld();
            FXGL.getGameTimer().runOnceAfter(() -> {
                TankGameApplication application = FXGL.getAppCast();
                TankGameApplication.clearGameWord();
                MapFactory.MapEntity[] mapEntities;
                application.levelUp();
                if ((mapEntities = application.loadLevelMap()) != null) {
                    application.initLevel(mapEntities);
                } else {
                    application.resetLevel();
                    FXGL.getGameController().gotoMainMenu();
                }
            }, Duration.millis(NEXT_LEVEL_DELAY));
        }
    }
}
